﻿using System;
using System.Data;
using System.Windows.Forms;
using Home.Haushalt.Lib;
using Infragistics.Win.UltraWinCalcManager.FormulaBuilder;
using System.Drawing;
using Infragistics.Win.UltraWinGrid;
using System.IO;
using System.Collections.Generic;
using Home.Haushalt.Lib.Element;
using System.Collections;
using Infragistics.Win;
using System.Reflection;
using System.Linq;


namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UcGridForm : UserControl
    {
        #region EVENTS

        /// <summary></summary>
        public event EventHandler<HEventArgs> OnStatusChanged;

        /// <summary></summary>
        public event EventHandler<HEventArgs> OnLayoutChanged;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        protected void OnStatusChangedApply(HEventArgs args)
        {
            if (OnStatusChanged != null)
                OnStatusChanged(this, args);
        }

        #endregion EVENTS

        /// <summary></summary>
        private DefaultColumnSets _columnSet = DefaultColumnSets.GridDefault;

        /// <summary></summary>
        protected DefaultColumnSets ColumnSet
        {
            get { return _columnSet; }
            set { _columnSet = value; }
        }

        /// <summary></summary>
        private string _layoutName = DefaultColumnSets.GridDefault.ToString();

        /// <summary></summary>
        public string LayoutName
        {
            get { return _layoutName; }
            set { _layoutName = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public UcGridForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="columnSet"></param>
        /// <param name="layoutName"></param>
        /// <returns></returns>
        public static UcGridForm Instatiate(Type elementType, DefaultColumnSets columnSet, string layoutName)
        {
            UcGridForm ucGridForm = null;

            switch (elementType.Name)
            {
                case "Document":
                    ucGridForm = new UcGridFormDocument(columnSet, layoutName);
                    break;

                default:
                    ucGridForm = new UcGridFormElement(elementType, columnSet, layoutName);
                    break;
            }

            return ucGridForm;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gridFormTypeName"></param>
        /// <returns></returns>
        public static UcGridForm Instatiate(string gridFormTypeName)
        {
            UcGridForm ucGridForm = null;

            Type gridFormType = Assembly.GetExecutingAssembly().GetTypes().Where(item=>item.Name.Equals(gridFormTypeName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
            if (gridFormType != null)
            {
                ucGridForm = (UcGridForm)Activator.CreateInstance(gridFormType);
            }

            return ucGridForm;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnSet"></param>
        /// <param name="layoutName"></param>
        public UcGridForm(DefaultColumnSets columnSet, string layoutName)
        {
            InitializeComponent();

            ColumnSet = columnSet;
            LayoutName = layoutName;

            BaseGrid.AllowExpertFilter(this.components);

            BaseGrid.OnElementDeleted += new EventHandler<HEventArgs>(grid_OnStatusMessage);
            BaseGrid.OnElementDeleting += new EventHandler<HEventArgs>(grid_OnStatusMessage);
            BaseGrid.OnStatusMessage += new EventHandler<HEventArgs>(grid_OnStatusMessage);
            BaseGrid.OnLayoutChanged += new EventHandler<HEventArgs>(grid_OnLayoutChanged);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_OnLayoutChanged(object sender, HEventArgs e)
        {
            if (OnLayoutChanged != null)
                OnLayoutChanged(this, e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_OnStatusMessage(object sender, HEventArgs e)
        {
            if (OnStatusChanged != null)
                OnStatusChanged(this, e);
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void DataBind() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuCloseAll_Click(object sender, EventArgs e)
        {
            WindowController.CloseAll();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void statusIco_DoubleClick(object sender, EventArgs e)
        {
            Journal.Show(false);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuSaveAllChanges_Click(object sender, EventArgs e)
        {
            DAO.Save();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuFile_DropDownOpening(object sender, EventArgs e)
        {
            //mnuSaveAllChanges.Enabled = DAO.DataChanged;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuDecryptFile_Click(object sender, EventArgs e)
        {
            if (ofdCryptFile.ShowDialog() == DialogResult.OK)
            {
                DAO.Credential.CurrentCry.FileDecrypt(ofdCryptFile.FileName);

                Chips.ShowExplorerAtFile(ofdCryptFile.FileName);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mnuEncryptFile_Click(object sender, EventArgs e)
        {
            if (ofdCryptFile.ShowDialog() == DialogResult.OK)
            {
                DAO.Credential.CurrentCry.FileEncrypt(ofdCryptFile.FileName);

                Chips.ShowExplorerAtFile(ofdCryptFile.FileName);
            }
        }
    }
}
